package week13_NetWork;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPServer {
	public static void main(String[] args) {
		try (DatagramSocket socket = new DatagramSocket(9999);) {
			byte[] buf = new byte[256];
			DatagramPacket packet = new DatagramPacket(buf, buf.length);
			while (true) {
				socket.receive(packet);
				InetAddress address = packet.getAddress();
				int port = packet.getPort();
				String data = new String(packet.getData()).trim();
				System.out.println("receive data from " + address + " at port " + port);
				System.out.println("data=" + data);
				if (data.trim().equalsIgnoreCase("quit"))
					break;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
